Crate spinning_top
source ·Expand description
Provides simple spinlocks based on the abstractions provided by the lock_api
crate.
Examples
Use Spinlock
for mutual exclusion:
use spinning_top::Spinlock;
fn main() {
let data = String::from("Hello");
// Wrap some data in a spinlock
let spinlock = Spinlock::new(data);
// Lock the spinlock to get a mutex guard for the data
let mut locked_data = spinlock.lock();
// The guard implements the `Deref` trait, so we can use it like a `&String`
assert_eq!(locked_data.as_str(), "Hello");
// It also implements `DerefMut` so mutation is possible too. This is safe
// because the spinlock ensures mutual exclusion
locked_data.make_ascii_uppercase();
assert_eq!(locked_data.as_str(), "HELLO");
// the guard automatically frees the lock at the end of the scope
}
Use RwSpinlock
if you need a readers-writer lock:
use spinning_top::RwSpinlock;
let lock = RwSpinlock::new(5);
// many reader locks can be held at once
{
let r1 = lock.read();
let r2 = lock.read();
assert_eq!(*r1, 5);
assert_eq!(*r2, 5);
} // read locks are dropped at this point
// only one write lock may be held, however
{
let mut w = lock.write();
*w += 1;
assert_eq!(*w, 6);
} // write lock is dropped here
Re-exports
pub use lock_api;
Modules
- Type aliases for guards.
- Relax strategies.
Structs
- A simple, spinning, read-preferring readers-writer lock.
- Provides mutual exclusion based on spinning on an
AtomicBool
.
Type Aliases
- A mutual exclusion (Mutex) type based on busy-waiting with exponential backoff.
- A
lock_api::RwLock
based onRawRwSpinlock
. - A mutual exclusion (Mutex) type based on busy-waiting.